﻿using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
	public string circleTagName;
	public string squareTagName;

	public float rayDistance = 0f;
	public int score = 0;
	public float gameTime = 20.0f;
	public float loadWaitTime = 3.0f;
	public int numberOfPointsToWin = 15;
	private Enemy enemy;

	//public static int finalScore=0;
	// Use this for initialization
	void Start () {
		//manager = GameObject.FindObjectOfType<GameManager> ();
		InvokeRepeating("CountDown", 1.0f, 1.0f);
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetMouseButtonDown(0)) {
			RaycastHit hit;
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //get the mouse position

			if (Physics.Raycast(ray, out hit, rayDistance)) {
				Debug.Log (hit.transform.tag);
				if ((hit.transform.renderer.enabled)) {
					if (hit.transform.tag == circleTagName) {
						CircleEnemy enemyScript = hit.transform.GetComponent<CircleEnemy>();
						enemyScript.numberOfClicks -= 1;
						if (enemyScript.numberOfClicks == 0) {
							score += enemyScript.enemyPoint;
						}
					}
					if (hit.transform.tag == squareTagName) {
						SquareEnemy enemyScript = hit.transform.GetComponent<SquareEnemy>();
						enemyScript.numberOfClicks -= 1;
						if (enemyScript.numberOfClicks == 0) {
							score += enemyScript.enemyPoint;
						}
					}


				} else {
					//not an enemy
				}
			}
		}
	}
	// Subtract 1 from the game timer every second
	void CountDown() {
		if (--gameTime == 0) {
			CancelInvoke("CountDown");
			//manager.GetComponent<GameManager>().gameScore=score;
			//finalScore=score;
			if (score >= numberOfPointsToWin) {
				Application.LoadLevel("WinScreen");
			} else {
				Application.LoadLevel("LoseScreen");
			}
		}
	}
	
	void OnGUI() {
		GUI.Label(new Rect(10,10,100,20), "Score: " + score.ToString());
		GUI.Label(new Rect(10,25,100,35), "Time: " +gameTime.ToString());
	}
}
